package com.cetccst.securitysuite.network;

import android.content.Context;
import android.text.TextUtils;

import cn.com.cetccst.network.IRetrofitClient;
import cn.com.cetccst.network.RetrofitManager;
import cn.com.cetccst.network.log.IHttpLog;
import com.cetccst.securitysuite.network.respone.BaseResponse;
import com.cetccst.securitysuite.network.respone.TokenResponse;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Retrofit;

public class LoginHttpRequester {

    private static final String MEDIA_TYPE_JSON = "application/json;charset=UTF-8";
    private static IRetrofitClient retrofitClient;
    private static Retrofit retrofit;
    private static IHttpLog httpLogConfig;
    private static LoginHttpRequester httpRequester;
    private static ILoginApiService apiService;

    public static void initialize(Context context) {
        if (retrofitClient == null) {
            retrofitClient = new RetrofitManager.Builder(context, "http://10.100.241.152:8080/").setLogLevel(IHttpLog.Level.BODY).build();
            retrofit = retrofitClient.getRetrofit();
            httpLogConfig = retrofitClient.getHttpLogConfig();
            apiService = retrofit.create(ILoginApiService.class);
        }
    }

    public static IHttpLog getHttpLogConfig() {
        return httpLogConfig;
    }

    private LoginHttpRequester() {

    }

    public static LoginHttpRequester getInstance() {
        if (httpRequester == null) {
            httpRequester = new LoginHttpRequester();
        }
        return httpRequester;
    }

    public String getErrorPage() {
        return retrofit.baseUrl() + "api/oops.html";
    }

    private void addHeaderIfNecessary(Request orgRequest, Request.Builder newRequest, String headerName, String headerContent) {
        if (TextUtils.isEmpty(orgRequest.header(headerName)) && !TextUtils.isEmpty(headerContent)) {
            newRequest.header(headerName, headerContent);
        }
    }

    public void login(HttpCallback<TokenResponse> callback) {
        Call<TokenResponse> call = apiService.login(requestBody(new Object(), MEDIA_TYPE_JSON), "15012345678", "password");
        call.enqueue(callback);
    }

    public void logout(HttpCallback<BaseResponse> callback) {
        Call<BaseResponse> call = apiService.logout();
        call.enqueue(callback);
    }

    public void postBasicData(String[] data, HttpCallback<BaseResponse> callback) {
        Map<String, String[]> body = new HashMap<>();
        body.put("basic_data", data);
        Call<BaseResponse> call = apiService.postBasicData(requestBody(body, MEDIA_TYPE_JSON));
        call.enqueue(callback);
    }

    private RequestBody requestBody(Object body, String type) {
        MediaType mediaType = MediaType.parse(type);
        String bodyStr = new Gson().toJson(body);
        return RequestBody.create(mediaType, bodyStr);
    }
}
